package com.api.gbhg.huangpeng.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

@Path("/gbys")
/**
 * huangwm 修改
 */
public class YusuanApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @Path("/yusuanlaiyuan")
    @POST
    @Produces({"application/json"})
    public Map<String, Object> yusuanlaiyuan(Map<String, String> param)
    {
        String lcidList = (String)param.get("lcid");
        String[] lcidarr = lcidList.split(",");
        String srlxList = (String)param.get("zcqd");
        String[] srlxarr = srlxList.split(",");
        String indexList = (String)param.get("index");
        String[] indexarr = indexList.split(",");
        Map<String, Object> result = new HashMap<>();
        List< Map<String, Object>> datas = new ArrayList<>();
        for (int i = 0; i < lcidarr.length; i++)
        {
            Map<String, Object> map = new HashMap();
            RecordSet rs1 = new RecordSet();
            String sql1 = "SELECT zyczbksr,srlx,yslylx,zcqd,(select mc from uf_srysfl where id=zcqd)as zcqdmc ,dfczbkxm," +
                    "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc,(select id from uf_gxmdygnkm where hpssjid=zyczbksr and xmlcid=lcid)AS xzkmdm," +
                    "(select dygnkmmc from uf_gxmdygnkm where hpssjid=zyczbksr and xmlcid=lcid)AS xzkmdmmc," +
                    "(select kmdm from uf_gxmdygnkm where hpssjid=zyczbksr and  xmlcid=lcid) AS field1 from uf_jingfeilaiyuan " +
                    "where lcid=" + lcidarr[i] + "";
            rs1.execute(sql1);
            rs1.next();
            rs1.writeLog("yusuanlaiyuan："+sql1+">>"+rs1.getCounts());

            if(rs1.getCounts()>1) {
                map.put("index", indexarr[i]);
                map.put("ysly", "");
                map.put("zcqd", "");
                map.put("xzkmdm", "");
                map.put("xzkmdmmc", "");
                map.put("zcgnkm", "");
                map.put("fzysrfl", "");
                map.put("fzysrflmc", "");
                map.put("zcdfczbkxm", "");
                map.put("zcdfczbkxmmc", "");
                map.put("gnkmdm", "");
                datas.add(map);
            }else {
                map.put("index", indexarr[i]);
                map.put("ysly", rs1.getString("yslylx").indexOf("上年")>=0?"1":"0");
                map.put("zcqd",  rs1.getString("srlx"));
                map.put("xzkmdm", rs1.getString("xzkmdm"));
                map.put("xzkmdmmc", rs1.getString("xzkmdmmc"));
                map.put("zcgnkm", getZcgnkm(rs1.getString("zyczbksr")));
                map.put("fzysrfl", rs1.getString("zcqd"));
                map.put("fzysrflmc", rs1.getString("zcqdmc"));
                map.put("zcdfczbkxm", rs1.getString("dfczbkxm"));
                map.put("zcdfczbkxmmc", rs1.getString("dfczbkxmmc"));
                map.put("gnkmdm", rs1.getString("field1"));
                datas.add(map);
            }
        }

        result.put("msg", "获取返回成功");
        result.put("apiStatus", true);
        result.put("result", datas);
        logger.info(JSONObject.toJSONString(datas));
        return result;
    }

    @Path("/zhichuqudao")
    @POST
    @Produces({"application/json"})
    public Map<String, Object> zhichuqudao(Map<String, String> param)
    {
        String lcid = (String)param.get("lcid");
        String srlx = (String)param.get("srlx");
        Map<String, Object> result = new HashMap();
        RecordSet rs1 = new RecordSet();
        String sql1 = "SELECT zyczbksr,yslylx,zcqd,(select mc from uf_srysfl where id=zcqd)as zcqdmc ,dfczbkxm," +
                "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc,(select id from uf_gxmdygnkm where hpssjid=zyczbksr and xmlcid=lcid)AS xzkmdm," +
                "(select dygnkmmc from uf_gxmdygnkm where hpssjid=zyczbksr and xmlcid=lcid)AS xzkmdmmc," +
                "(select kmdm from uf_gnkm where id=gnkm) AS field1 from uf_jingfeilaiyuan " +
                "where lcid=" + lcid + " and srlx=" + srlx + "";
        rs1.execute(sql1);
        rs1.next();
        rs1.writeLog("zhichuqudao："+sql1+">>"+rs1.getCounts());
        if(rs1.getCounts()>1){
            result.put("ysly", "");
            result.put("srlx", srlx);
            result.put("zcqd", "");
            result.put("xzkmdm", "");
            result.put("xzkmdmmc", "");
            result.put("zcgnkm", "");
            result.put("fzysrfl", "");
            result.put("fzysrflmc", "");
            result.put("zcdfczbkxm", "");
            result.put("zcdfczbkxmmc", "");
        }else{
            result.put("ysly", rs1.getString("yslylx").indexOf("上年")>=0?"1":"0");
            result.put("srlx", srlx);
            result.put("zcqd",   srlx);
            result.put("xzkmdm", rs1.getString("xzkmdm"));
            result.put("xzkmdmmc", rs1.getString("xzkmdmmc"));
            result.put("zcgnkm", getZcgnkm(rs1.getString("zyczbksr")));
            result.put("fzysrfl", rs1.getString("zcqd"));
            result.put("fzysrflmc", rs1.getString("zcqdmc"));
            result.put("zcdfczbkxm", rs1.getString("dfczbkxm"));
            result.put("zcdfczbkxmmc", rs1.getString("dfczbkxmmc"));
        }
        result.put("msg", "获取返回成功");
        result.put("apiStatus", true);
        return result;
    }

    public String getZcgnkm(String czsr){
        logger.info("获取功能科目代码："+ czsr);
        if("".equals(czsr) || czsr == ""){
            return "";
        }
        RecordSet rs = new RecordSet();
        String kmj = new BaseBean().getPropValue("Verification","yj");
        if(czsr.split("_")[0].equals(kmj)){
            rs.execute("select gnkmmc from uf_zyczbksr_km where id="+czsr.split("_")[1]);
            rs.next();
            return rs.getString("gnkmmc");
        }else{
            rs.execute("select (select gnkmmc from uf_zyczbksr_km where id=szgnkm)as gnkmmc  from uf_zyczbksr_xj where id="+czsr.split("_")[1]);
            rs.next();
            return rs.getString("gnkmmc");
        }
    }


}
